import java.sql.*;
import java.util.Vector;


public class memberManager {
	
	// data control
	private Vector<member> members;
	private member currentMember;
	private int memberIdCounter;
	
	
	public memberManager(){
		members = new Vector<member>(100);
		memberIdCounter = 0;
	}
	
	// add member
	public boolean addMember(member newMember){
		if (!members.contains(newMember)){
			members.add(newMember);
			memberIdCounter++;
			return true;
		}
		return false;
	}
	
	// edit member
	public void update(member m){
		for (int i=0; i<members.size(); i++)
			if (members.get(i).getMemberID() == m.getMemberID())
				members.set(i, m);
	}
	
	public Vector<member> getMembers() {return members;}
	
	// find member by member name
	public member findMemberByName(String name){
		for (int i=0; i<members.size(); i++)
			if (members.get(i).getName().equals(name))
				return members.get(i);
		return null;
	}
	
	// delete member
	public void deleteMember(int i){
		members.remove(i);
	}
	
	// get member id
	public int getMemberId(){
		return memberIdCounter;
	}
	// load from database
	public void loadMembers()
	{
		try{
			Connection conn=ConnectionUtils.getConnection();						           	            
	        PreparedStatement s=conn.prepareStatement("select * from member",
	                    ResultSet.TYPE_SCROLL_SENSITIVE,
	                    ResultSet.CONCUR_UPDATABLE);
	    	ResultSet rs=s.executeQuery();
	    	int memberId=0;
	    	String name;
	    	long phone;
	    	int age;
	    	String gender;
	    	String idType;
	    	String id;
	    	String currentState;
	    	String startTime;
	        if(rs.next()){
	           rs.beforeFirst();    
	           while(rs.next()){	        	   
	        	   memberId = rs.getInt(1);
	        	   name = rs.getString(2);
	        	   phone = rs.getLong(3);                
	        	   age = rs.getInt(4);
	        	   gender = rs.getString(5);
	        	   idType = rs.getString(6);
	        	   id = rs.getString(7);
	        	   currentState = rs.getString(8);
	        	   startTime = rs.getString(9);
	        	   
	            	currentMember= new member(memberId,name,phone,age,gender,idType,id,currentState,startTime);
	            	members.add(currentMember);
	            	System.out.println("+1");
	            }
	           memberIdCounter=(memberId+1);
	            System.out.println("all users information are loaded");
	        }
		    rs.close();
		    conn.close();	            
			}catch(Exception e){
	    		e.printStackTrace();
	    	}
		}
	// update user info
		public void updateMember(member m){			
			try{
				for (int i=0; i<members.size(); i++)
					if (members.get(i).getMemberID()==m.getMemberID())
						members.set(i, m);
			    Connection conn=ConnectionUtils.getConnection();
			    PreparedStatement s=conn.prepareStatement("select count(*) from member where memberId= ?");
			    s.setInt(1, m.getMemberID());
			    ResultSet rs = s.executeQuery();
			    if(rs.next()){
			    	if(rs.getInt(1)>0){				  
			    		rs.close();
			    		PreparedStatement ss=conn.prepareStatement("DELETE from member where memberId= ?");
			    		ss.setInt(1, m.getMemberID());
			    		int deletecount = ss.executeUpdate();
			    		System.out.println(deletecount + " row of member data is updated");
			    	}				    	    
			    }
		    
			    conn.setAutoCommit(false);
			    PreparedStatement stmt = conn.prepareStatement("INSERT INTO member VALUES (?,?,?,?,?, ?,?,?,?)");		        
		        stmt.setString(1,  null);
		        stmt.setString(2,  m.getName());
		        stmt.setLong(3,  m.getPhone());
		        stmt.setInt(4,  m.getAge());
		        stmt.setString(5,  m.getGender());
		        stmt.setString(6,  m.getIdType());
		        stmt.setString(7,  m.getId());
		        stmt.setString(8,  m.getCurrentState());
		        stmt.setString(9,  m.getStartTime());		        		        	
		    
		    stmt.addBatch();
	        stmt.executeBatch();
	        conn.commit(); 
	        conn.close();
			}catch(Exception e){
	    		e.printStackTrace();
	    	}	
		}
}
